package com.njrsrc.android.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.njrsrc.R;
import com.njrsrc.android.adapters.NewsListAdapter;
import com.njrsrc.android.bean.NewsInfo;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.android.library.ptr.PullToRefreshBase;
import com.njrsrc.android.library.ptr.PullToRefreshBase.Mode;
import com.njrsrc.android.library.ptr.PullToRefreshBase.OnRefreshListener;
import com.njrsrc.android.library.ptr.PullToRefreshListView;
import com.njrsrc.android.library.vpi.BaseSampleActivity;
import com.njrsrc.android.tasks.NewsTask;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.url.RequestUrlProvider;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

/**
 * 招聘会
 * 
 * @author Administrator
 * 
 */
public class NewsActivity extends BaseSampleActivity implements OnClickListener {
	private TitleBar mBar;
	private UserInfo userInfo;
	private ProgressDialog progressDialog;
	private PullToRefreshListView jobListView;
	private NewsListAdapter adapter;
	private int PAGE_ID = 1;
	/** 每页显示的条数 */
	private static final int COUNT = 10;
	private boolean loadingEnd = false;
	private ArrayList<NewsInfo> newsList;
	private String requestUrl;
	
	private HashMap<String, String> types = new HashMap<String, String>();

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MsgID.NEWS_QUERY_ERROR:
				Helpers.closeProgressDialog(progressDialog);
				Toast.makeText(NewsActivity.this, 
						getResources().getString(R.string.connectError), 
						Toast.LENGTH_SHORT).show();
				break;
			case MsgID.NEWS_QUERY_FAIL:
				Helpers.closeProgressDialog(progressDialog);
				break;
			case MsgID.NEWS_QUERY_SUCCESS:
				Helpers.closeProgressDialog(progressDialog);
				jobListView.onRefreshComplete();
				adapter.setData(newsList);
				adapter.notifyDataSetChanged();
				clear();
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_job_fair);
		types.put("00", "最新资讯");
		types.put("35", "政策法规");
		types.put("40", "市场信息");
		types.put("71", "才经动态");
		initView();
	}

	private void initView() {
		mBar = (TitleBar) findViewById(R.id.activity_title_bar);
		mBar.setTitleText(types.get("00"));
		mBar.setTag("00");
		mBar.setRightBg(R.drawable.ico_category);
		mBar.right.setOnClickListener(this);
		mBar.back.setOnClickListener(this);
		jobListView = (PullToRefreshListView) findViewById(R.id.job_listView);
		jobListView.setScrollingWhileRefreshingEnabled(!jobListView.isScrollingWhileRefreshingEnabled());
		jobListView.setMode(Mode.PULL_FROM_END);
		jobListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
 			@Override
 			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
 				if(!loadingEnd)
 				{
 					initData(mBar.getTag().toString());
 				}
 			}
 		});
		
		adapter = new NewsListAdapter(NewsActivity.this);
		jobListView.setAdapter(adapter);
		
		progressDialog = Helpers.showProgressDialog(NewsActivity.this, R.string.loading);
		initData(mBar.getTag().toString());
		
		jobListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				String newsId = (String) arg1.getTag(R.id.news_type);
				Intent intent = new Intent();
				intent.setClass(NewsActivity.this, NewsDetailActivity.class);
				Bundle data = new Bundle();
				data.putString("newsId", newsId);
				intent.putExtras(data);
				startActivity(intent);
				
			}
			
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	private void initData(String newsType)
	{
		List<String> params = new ArrayList<String>();
		params.add(AppApplication.mToken);
		params.add(AppApplication.mUserID);
		params.add(newsType);
		params.add("0");
		params.add(PAGE_ID + "");
		params.add(COUNT + "");
		requestUrl = RequestUrlProvider.NEWS_LIST;
		String url = Helpers.combinaStr(requestUrl, params);
		userInfo = new UserInfo();
		newsList = new ArrayList<NewsInfo>();
		Object[] args = new Object[4];
		args[0] = mHandler;
		args[1] = url;
		args[2] = userInfo;
		args[3] = newsList;
		new NewsTask().execute(args);
	}
	
	private void clear()
	{
		if(newsList == null || newsList.size() == 0 || newsList.size() < COUNT)
        {
        	loadingEnd = true;
        	jobListView.setMode(Mode.DISABLED);
        }else
        {
        	PAGE_ID++;
        }
		newsList.clear();
		newsList = null;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tb_ibtn_back:
			finish();
			break;
		case R.id.tb_ibtn_right:
			NewsTypeDialog newsTypeDialog = new NewsTypeDialog(this,R.style.myDialogStyle);
			newsTypeDialog.show();
			break;
		default:
			break;
		}
	}
	
	/**
	 * 版面名称对话框
	 * @author Oliverzhu
	 *
	 */
	private class NewsTypeDialog extends Dialog{
		private ViewGroup layout;
		private Button lateNews;
		private Button policy;
		private Button marketInformation;
		private Button abilityTrends;
		
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			layout = (ViewGroup) View.inflate(NewsActivity.this, R.layout.dialog_newstype, null);
			setContentView(layout);
			initComponents();
			setComponentListener();
		}

		/**
		 * @param context
		 * @param seekBar 
		 * @param contentDelegate 
		 * @param name 
		 */
		public NewsTypeDialog(Context context, List<String> names) {
			super(context);
		}
		/**
		 * @param context
		 */
		public NewsTypeDialog(Context context) {
			super(context);
		}

		/**
		 * @param context
		 * @param cancelable
		 * @param cancelListener
		 */
		public NewsTypeDialog(Context context, boolean cancelable,
				OnCancelListener cancelListener) {
			super(context, cancelable, cancelListener);
		}

		/**
		 * @param context
		 * @param theme
		 */
		public NewsTypeDialog(Context context, int theme) {
			super(context, theme);
		}
		
		private void initComponents()
		{
			lateNews = (Button) layout.findViewById(R.id.lateNews);
			policy = (Button) layout.findViewById(R.id.policy);
			marketInformation = (Button) layout.findViewById(R.id.marketInformation);
			abilityTrends = (Button) layout.findViewById(R.id.abilityTrends);
			lateNews.setTag("00");
			policy.setTag("35");
			marketInformation.setTag("40");
			abilityTrends.setTag("71");
		}
		
		private View.OnClickListener listener = new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				NewsTypeDialog.this.dismiss();
				String barTag = (String) mBar.getTag();
				String viewTag = (String) v.getTag();
				if(barTag.equals(viewTag))
				{
					return;
				}
				PAGE_ID = 1;
				jobListView.setMode(Mode.PULL_FROM_END);
				loadingEnd = false;
				adapter.list.clear();
				mBar.setTitleText(types.get(viewTag));
				mBar.setTag(viewTag);
				NewsActivity.this.initData(mBar.getTag().toString());
			}

		};
		
		
		private void setComponentListener()
		{
			lateNews.setOnClickListener(listener);
			policy.setOnClickListener(listener);
			marketInformation.setOnClickListener(listener);
			abilityTrends.setOnClickListener(listener);
			
		}
		
	}
}